package com.aerotechnic.controller;

import javax.servlet.http.HttpServletRequest;
import org.hibernate.Session;
import org.springframework.ui.ModelMap;

import com.aerotechnic.entities.Event;
import com.aerotechnic.entities.MacroTacheBiblio;
import com.aerotechnic.service.interfaces.MacroTacheBiblioBoIn;
import com.aerotechnic.util.HibernateUtil;
import com.dhtmlx.planner.DHXEv;
import com.dhtmlx.planner.DHXEventsManager;
import com.dhtmlx.planner.DHXStatus;
import com.dhtmlx.planner.data.DHXCollection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CustomEventsManager extends DHXEventsManager {

	MacroTacheBiblioBoIn macroTacheBiblio;

	public CustomEventsManager(HttpServletRequest request,
			MacroTacheBiblioBoIn macroTacheBiblio) {
		super(request);
		this.macroTacheBiblio = macroTacheBiblio;
	}

	public Iterable<DHXEv> getEvents() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<DHXEv> evs = new ArrayList<DHXEv>();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			evs = session.createCriteria(Event.class).list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}

		return evs;
	}

	@Override
	public DHXStatus saveEvent(DHXEv event, DHXStatus status) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();

			if (status == DHXStatus.UPDATE){
				// get MacroTache by intitule

				MacroTacheBiblio fetchedMac = macroTacheBiblio
						.findbyIntitule(((Event) event).getIntitule());
				//get description

				String macDesc = fetchedMac.getDescription();
				
//				System.out.println("affichage de la description ===>"+macDesc);  
				((Event) event).setDescription(macDesc);
				
				session.update(event);   
			}
				
			else if (status == DHXStatus.DELETE)
				session.delete(event);
			else if (status == DHXStatus.INSERT) {
				// get MacroTache by intitule

				MacroTacheBiblio fetchedMac = macroTacheBiblio
						.findbyIntitule(((Event) event).getIntitule());
				//get description

				String macDesc = fetchedMac.getDescription();
				
				System.out.println("affichage de la description ===>"+macDesc);  
				((Event) event).setDescription(macDesc);
				session.save(event);  
			}

			session.getTransaction().commit();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return status;
	}

	@Override
	public DHXEv createEvent(String id, DHXStatus status) {
		return new Event();
	}

	@Override
	public HashMap<String, Iterable<DHXCollection>> getCollections() {
		ArrayList<DHXCollection> tables_tree = new ArrayList<DHXCollection>();
		Table c1 = new Table("specialite", "SPECIALITE", true);
		// Table c2 = new Table("3-6", "3-6 people", 5, true);

		c1.addChild(new Table("eir", "EIR", true));
		c1.addChild(new Table("circuit", "CIRCUIT", true));
		c1.addChild(new Table("sstt", "SSTT", true));
		c1.addChild(new Table("cabine", "CABINE", true));
		// c2.addChild(new Table("table5", "Table 5", 4, true));
		// c2.addChild(new Table("table6", "Table 6", 4, true));
		// c2.addChild(new Table("table7", "Table 7", 6, false));
		// c2.addChild(new Table("table8", "Table 8", 6, false));

		tables_tree.add(c1);
		// tables_tree.add(c2);

		HashMap<String, Iterable<DHXCollection>> c = new HashMap<String, Iterable<DHXCollection>>();
		c.put("tables_tree", tables_tree);

		// les option qui seront inclut dans le champs specialite
		ArrayList<DHXCollection> table_lightbox = new ArrayList<DHXCollection>();
		table_lightbox.add(new Table("eir", "EIR", true));
		table_lightbox.add(new Table("circuit", "CIRCUIT", true));
		table_lightbox.add(new Table("sstt", "SSTT", true));
		table_lightbox.add(new Table("cabine", "CABINE", true));
		c.put("tables", table_lightbox);

		// les option qui seront inclut dans le champs intitule

		ArrayList<DHXCollection> table_intitule = new ArrayList<DHXCollection>();
		List<MacroTacheBiblio> taches = macroTacheBiblio.findAll();
		for (int i = 0; i < taches.size(); i++) {
			table_intitule.add(new Table(taches.get(i).getIntitule(), taches
					.get(i).getIntitule(), true));
		}

		// table_intitule.add(new Table("tache1", "tache1", true));
		// table_intitule.add(new Table("tache2", "tache2" , true));
		// table_intitule.add(new Table("tache3", "tache3", true));

		c.put("tables-intitule", table_intitule);
		return c;
	}
}
